﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
    public partial class CriteriaJobsView : IBackButtonController
    {
        private readonly SelectorHelper<Locality> _regionSelector = new SelectorHelper<Locality>();
        private readonly SelectorHelper<District> _districtSelector = new SelectorHelper<District>();
        private readonly SelectorHelper<JobCategory> _jobCategorySelector = new SelectorHelper<JobCategory>();
        private readonly SelectorHelper<string> _selector = new SelectorHelper<string>();
        private readonly CriteriaJobsViewModel _viewModel;

        public CriteriaJobsView(CriteriaJobsViewModel criteria)
        {
            InitializeComponent();
            _viewModel = criteria;
            DataContext = _viewModel;
            VisualStateManager.GoToState(this, "HiddenState", false);

            LayoutUpdated += (e, s) => VisualStateManager.GoToState(this, "VisibleState", true);
        }

        public bool AnySelectorOpen
        {
            get
            {
                return _regionSelector.IsOverlayOpen ||
                        _districtSelector.IsOverlayOpen ||
                       _jobCategorySelector.IsOverlayOpen ||
                       _selector.IsOverlayOpen;
            }
        }

        public Action RefreshAction { get; set; }

        private void RegionGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _regionSelector.Items = _viewModel.Regions;
            _regionSelector.OpenPopup(LayoutRoot, () =>
            {
                SelectorItem<Locality> selected = _regionSelector.Selected;
                if (selected == null) return;

                _viewModel.SelectedRegion = selected.Item;
            });

        }

        private void DistrictGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _districtSelector.Items = _viewModel.Districts;
            _districtSelector.OpenPopup(LayoutRoot, () =>
            {
                SelectorItem<District> selected = _districtSelector.Selected;
                if (selected == null) return;

                _viewModel.SelectedDistrict = selected.Item;
            });

        }

        private void JobCategoryGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _jobCategorySelector.Items = _viewModel.JobCategories;
            _jobCategorySelector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _jobCategorySelector.Selected;
                if (selected == null) return;

                _viewModel.SelectedJobCategory = selected.Item;
            });

        }

        private void JobSubCategoryGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _jobCategorySelector.Items = _viewModel.JobSubCategories;
            _jobCategorySelector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _jobCategorySelector.Selected;
                if (selected == null) return;

                _viewModel.SelectedJobSubCategory = selected.Item;
            });
        }

        private void PayFrom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _selector.Items = _viewModel.PayItems;
            _selector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _selector.Selected;
                if (selected == null) return;

                _viewModel.SelectedPayFrom = selected;
            });
        }

        private void PayTo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _selector.Items = _viewModel.PayItems;
            _selector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _selector.Selected;
                if (selected == null) return;

                _viewModel.SelectedPayTo = selected;
            });
        }

        private void SortByGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _selector.Items = _viewModel.SortByItems;
            _selector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _selector.Selected;
                if (selected == null) return;

                _viewModel.SelectedSortBy = selected;
            });
        }

        private void JobTypeGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _selector.Items = _viewModel.JobTypeItems;
            _selector.OpenPopup(LayoutRoot, () =>
            {
                var selected = _selector.Selected;
                if (selected == null) return;

                _viewModel.SelectedJobType = selected;
            });
        }

        public bool CanClose()
        {
            return !AnySelectorOpen;
        }

        public void Close(Action onClosed)
        {
            VisualStateManager.GoToState(this, "HiddenVisualState", true);
            if (onClosed != null)
                TradeMeDispatcher.OnUIThread(300, onClosed);
        }

        public void CloseChild()
        {
            if (_regionSelector.IsOverlayOpen) _regionSelector.ClosePopup();
            if (_districtSelector.IsOverlayOpen) _regionSelector.ClosePopup();
            if (_jobCategorySelector.IsOverlayOpen) _jobCategorySelector.ClosePopup();
            if (_selector.IsOverlayOpen) _selector.ClosePopup();
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            if (RefreshAction != null)
                RefreshAction();
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Reset();
        }

    }
}
